package com.lzkj.baotouhousingfund.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.coderc.library.utils.LogUtil;
import com.coderc.library.view.statusbar.StatusBarUtil;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.app.App;
import com.lzkj.baotouhousingfund.di.component.ActivityComponent;
import com.lzkj.baotouhousingfund.di.component.DaggerActivityComponent;
import com.lzkj.baotouhousingfund.di.module.ActivityModule;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by codeest on 2016/8/2.
 * MVP activity基类
 */
public abstract class BaseActivity<T extends BasePresenter> extends RxAppCompatActivity
        implements BaseView {

    @Inject
    protected T                         mPresenter;
    protected Activity                  mContext;
    private   Unbinder                  mUnBinder;
    public    App                       mApp;
    private   InputMethodManager        mInputMethodManager;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        initStatusBarDefColor();
        mApp = (App) getApplication();
        mUnBinder = ButterKnife.bind(this);
        mContext = this;
        initInject();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        App.getInstance().addActivity(this);
        initEventAndData();
        //如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            //注册到事件主线
            EventBus.getDefault().register(this);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void setToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(view -> onBackPressed());
    }

    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .appComponent(App.getAppComponent())
                .activityModule(getActivityModule())
                .build();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        mUnBinder.unbind();
        App.getInstance().removeActivity(this);
    }

    public boolean useEventBus() {
        return false;
    }

    @Override
    public void useNightMode(boolean isNight) {
        if (isNight) {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_NO);
        }
        recreate();
    }


    public void hideSoftInput(View view) {
        if (mInputMethodManager == null) {
            initInputMetodManager();
        }
        mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    private void initInputMetodManager() {
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    protected void initStatusBarDefColor() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorTheme), 5);
    }


    protected boolean filterException(Exception e) {
        if (e != null) {
            e.printStackTrace();
            LogUtil.i(e.getMessage());
            return false;
        } else {
            return true;
        }
    }


    protected abstract void initInject();

    protected abstract int getLayout();

    protected abstract void initEventAndData();
}